package com.abvnet.project.water.project.controller;

import com.abvnet.common.utils.StringUtils;
import com.abvnet.common.utils.file.FileUploadUtils;
import com.abvnet.common.utils.file.FileUtils;
import com.abvnet.framework.aspectj.lang.annotation.Log;
import com.abvnet.framework.aspectj.lang.enums.BusinessType;
import com.abvnet.framework.config.AbvnetConfig;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.framework.web.page.TableDataInfo;
import com.abvnet.project.water.project.domain.ProjectFlow;
import com.abvnet.project.water.project.service.IProjectFlowService;
import com.abvnet.project.water.sewage.service.ISewageFlowService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 项目流程信息操作处理
 * 
 * @author duj
 * @date 2019-07-02
 */
@Controller
@RequestMapping("/project/projectFlow")
@Slf4j
public class ProjectFlowController extends BaseController {
    private String prefix = "project/projectInfo";

    @Autowired
    private IProjectFlowService projectFlowService;

    @Autowired
    private ISewageFlowService sewageFlowService;
	
	@RequiresPermissions("project:projectFlow:list")
	@GetMapping()
	public String projectProgress()
	{
	    return prefix + "/projectFlow";
	}
	
	/**
	 * 查询处理工艺流程列表
	 */
	@RequiresPermissions("project:projectFlow:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ProjectFlow projectFlow) {
        startPage();
        List<ProjectFlow> list = projectFlowService.selectProjectFlowList(projectFlow);
        return getDataTable(list);
	}
	
    /**
     * 新增处理工艺流程
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }
	
	/**
	 * 新增保存处理工艺流程
	 */
	@RequiresPermissions("project:projectFlow:add")
	@Log(title = "处理工艺流程", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ProjectFlow projectFlow) {
        return toAjax(projectFlowService.insertProjectFlow(projectFlow));
	}

	/**
	 * 修改
	 */
    @GetMapping("/edit/{projectId}")
    public String edit(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        ProjectFlow projectFlow = projectFlowService.selectProjectFlowByProjectId(projectId);
        projectFlow.setFlowData(StringUtils.decryptString(projectFlow.getFlowData()));
        mmap.put("sfList",sewageFlowService.selectSewageFlowList(null));
        mmap.put("projectFlow", projectFlow);
        return prefix + "/flow";
    }
	
	/**
	 * 修改保存处理工艺流程
	 */
	@Log(title = "处理工艺流程", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ProjectFlow projectFlow) {
	    if(StringUtils.isNotEmpty(projectFlow.getFlowPicUrl())) {
            FileUtils.deleteFile(AbvnetConfig.getFlowPath()+projectFlow.getFlowPicUrl());
        }
		if(StringUtils.isNotEmpty(projectFlow.getOtherPicUrl())) {
			FileUtils.deleteFile(AbvnetConfig.getFlowPath()+projectFlow.getOtherPicUrl());
		}
        try {
            String flowPicUrl = FileUploadUtils.upload(AbvnetConfig.getFlowPath(), projectFlow.getPicData());
            String otherPicUrl = FileUploadUtils.upload(AbvnetConfig.getFlowPath(), projectFlow.getOtherPicData());
            projectFlow.setFlowPicUrl(flowPicUrl);
            projectFlow.setFlowData(StringUtils.encryptString(projectFlow.getFlowData()));
            if (StringUtils.isNotEmpty(projectFlow.getOtherPicData()))
            	projectFlow.setOtherPicUrl(otherPicUrl);
            if (StringUtils.isNotEmpty(projectFlow.getFlowIds())) {
				String ids = "";
				String[] idsArray = projectFlow.getFlowIds().split(",");
				for (String id : idsArray) {
					if (id.indexOf("-") == -1)
						ids += id + ",";
					else
						ids += id.substring(0, id.indexOf("-")) + ",";
				}
				projectFlow.setFlowIds(ids.substring(0, ids.lastIndexOf(",")));
			}
            return toAjax(projectFlowService.updateProjectFlow(projectFlow));
        } catch (Exception e) {
            log.error("更新处理工艺流程失败！", e);
            return error(e.getMessage());
        }
	}
	
	/**
	 * 删除项目进度
	 */
	@RequiresPermissions("project:projectFlow:remove")
	@Log(title = "处理工艺流程", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
        return toAjax(projectFlowService.deleteProjectFlowByIds(ids));
	}

}
